
##
## NOTE: You generally do not have to edit/change anything here.
##
## NOTE: There are some useful global variables that you can use
##       in scripts. Check out the declarations below. 
##
## This script initialises some global variables and creates collections of 
## objects for later use. The script has 'priority', meaning that it's the first
## script executed after starting the application. 
##


import bge
import bgl

BGL = bgl

scene = bge.logic.getCurrentScene()

# Create and save a first model and projection matrix
bge.modelMatrix = BGL.Buffer(BGL.GL_DOUBLE, [4, 4])
bge.projMatrix = BGL.Buffer(BGL.GL_DOUBLE, [4, 4])
BGL.glGetDoublev(BGL.GL_MODELVIEW_MATRIX, bge.modelMatrix);
BGL.glGetDoublev(BGL.GL_PROJECTION_MATRIX, bge.projMatrix);


# Scene description parameters, usually no need to change

bge.MainScene = scene       # The main scene (usually "Main")
bge.sun = ["Sun1", "Sun2"]  # Sun lights used in scene, these are affected by GUI
bge.selectObj = None        # Selected object
bge.handles = True          # show IK handles?
bge.showHotspots = True     # show hotspots?
bge.SplitScreen = bge.render.getWindowWidth()         #TODO: Remove


# State variables, used in run-time. Don't change or set

bge.mouse = [0,0,0]         
bge.div = [0,0,0]           # Difference between mouseclick and 3d point
bge.prevCords = [0,0,0]     # Last coordinates of 3d point
bge.stuck = 0               # True if user object connected to an object
bge.grab = 0                # True if user is dragging object
bge.track = 0               # Is GameObject tracking target?
bge.selob = None            # Object selected (if none, None)
bge.obj_z = 0               # Initial Z value for gluUnproject
bge.ik = 0                  # Initial state of inverse kinematics
bge.suspend = False         # True if GUI's are active

print (" ")
print ("Starting Virtual Persona environment...")
print ("Creating object collections...")

bge.hotspots = []
bge.statIcons = []
bge.cameras = []
bge.numGameObjects = 0

for obj in scene.objects:
    if 'gameObject' in obj:
        bge.numGameObjects += 1 
    if 'hotspot' in obj or 'hmi' in obj:
        bge.hotspots = bge.hotspots + [obj]
    if 'on' in obj:
        bge.statIcons = bge.statIcons + [obj]
    if 'p1' in obj:
        bge.p1Obj = bge.p1Obj + [obj]
    if 'p2' in obj:
        bge.p2Obj = bge.p2Obj + [obj]



#New camera array
for camera in scene.cameras:
    if camera.name != "__default__cam__":
        bge.cameras += [camera]

if str(scene.active_camera) == "__default__cam__":
    print ("Setting camera to first available camera")
    scene.active_camera = scene.cameras[0]


bge.cameras = sorted(bge.cameras, key=lambda KX_Camera: KX_Camera.name)


#Print report
print (" \-> Game Objects: ", bge.numGameObjects)
print (" \-> Hotspots: ", len(bge.hotspots))
print (" \-> Cameras: ", len(bge.cameras))



print ("--- End of init ---")
print (" ")
